package dynamic_handle;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.Clock;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import algorithm.Dynamic_credit;
import algorithm.Point_record;
import DB.DB_connection_pool;

public class Publish_dynamic extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		
		String dynamic_photo_path = request.getParameter("dynamic_photo_path");   //发布动态的图片url
		String dynamic_information_content = request.getParameter("dynamic_information_content");   //动态的类容
		
		Clock clock = Clock.systemUTC();
		String dynamic_information_id = String.valueOf((int)(Math.random()*900000)+100000)+clock.millis();  //动态编号
		
		String user_account = request.getSession().getAttribute("user_account").toString();   //用户账号
		java.sql.Date dynamic_information_date = new java.sql.Date(clock.millis());     //当前时间
		
		String public_dynamic = "insert into dynamic_information(dynamic_photo_path,dynamic_information_content,dynamic_information_id,user_account,dynamic_information_date) values(?,?,?,?,?)";
		
		JSONObject jsonObj = new JSONObject();
		
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement ps_select = conn.prepareStatement(public_dynamic);
			ps_select.setString(1, dynamic_photo_path);
			ps_select.setString(2, dynamic_information_content);
			ps_select.setString(3, dynamic_information_id);
			ps_select.setString(4, user_account);
			ps_select.setDate(5, dynamic_information_date);
			if(ps_select.executeUpdate()!=0){      //发布动态成功
				jsonObj.put("result", true);
				out.print(jsonObj.toString());
				
				Dynamic_credit dc = new Dynamic_credit();
				Map<String, Integer> map = dc.Get_credit(Dynamic_credit.publish);
				
				if(map.get("flag")==1){            //积分改变
				Point_record pr = new Point_record();
				pr.change_point(user_account,(double)(map.get("credit")));
				pr.point_record(user_account, Point_record.publish_event, (double)(map.get("credit")), true);
				}
				
			}else{
				jsonObj.put("result", false);
				out.print(jsonObj.toString());
			}
			
			conn.close();
			ps_select.close();
			out.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			jsonObj.put("result", false);
			out.print(jsonObj.toString());
			e.printStackTrace();
		}
		
		
		
		
		
		
		
	}

}
